//
//  LGSportOrderSearchController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/6.
//

#import "LGSportOrderSearchController.h"
#import "LGSportOrderDetailController.h"
#import "LGSportOrderTableViewCell.h"
@interface LGSportOrderSearchController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UIView *topView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , assign)NSInteger pageNo;
@end

@implementation LGSportOrderSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isfirst = NO;
    self.fd_prefersNavigationBarHidden = YES;
    [self.view addSubview:self.topView];
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.cancelBtn];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self.emptyView stopBufferAction];
    [self.searchTF becomeFirstResponder];
    [self textFieldValueChanged:self.searchTF];
}

-(void)cancelBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)requestData{
    self.pageNo = 1;
    self.dataArray = [NSMutableArray array];
    [self requestMoreData];
}

-(void)requestMoreData{
    [RequestUtil withPOST:@"/doctor/mall/sportList" parameters:@{@"pageNo":@(self.pageNo),@"pageSize":@"20",@"keyword":self.searchTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(code == 200){
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo ++;
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无订单" offset:0];
        }
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
        }
    }];
}


-(void)textFieldValueChanged:(UITextField *)textField{
    [self requestData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSportOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.orderDic = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGSportOrderDetailController *controller = [[LGSportOrderDetailController alloc]init];
    controller.recordid = LGNSString(tempDic[@"record_id"]);
    [self.navigationController pushViewController:controller animated:YES];
}


-(UIView *)topView{
    if(!_topView){
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, topBarHeight)];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    return _topView;
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(12), statusBarHeight+6, Screen_W-viewPix(72), viewPix(32))];
        _searchTF.backgroundColor = [UIColor colorWithString:@"#F3F3F3"];
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"输入患者姓名或订单编号查询";
        _searchTF.cornerRidus = 5;
        _searchTF.font = LGFont(15);
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = [self textFieldLeftView];
        [_searchTF addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _searchTF;
}


-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:kThemeColor imageName:@""];
        _cancelBtn.frame = CGRectMake(Screen_W-viewPix(60), kMinY(self.searchTF.frame), viewPix(60), viewPix(32));
        [_cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight) style:UITableViewStylePlain];
        [_tableView registerClass:[LGSportOrderTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = viewPix(148);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIView *)textFieldLeftView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(36), viewPix(32))];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchGray16"]];
    imageView.frame = CGRectMake(viewPix(12), viewPix(8), viewPix(16), viewPix(16));
    [baseView addSubview:imageView];
    return baseView;
}

@end
